package com.ease.concurrent.thread.state;

/**
 * 2. Join时处于什么状态？
 * 锁池状态 TIMED_WAITING, WAITING
 *
 * @author: chenhs
 * @date: Created in 9:53 2019/12/27
 **/
public class ThreadStateDemo2_Join {

    public static void main(String[] args) throws Throwable {


        Thread t1 = new Thread(()->{
            try {
                /** 模拟执行中【t1的状态为：TIMED_WAITING】 */
                Thread.sleep(10000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });


        Thread t2 = new Thread(()->{
            try {
                System.out.println(" t2中执行t1.join(5000L)...");
                /**（t2）等待t1五秒，5秒后不管怎么样继续往下执行！【此时t2是有限时间等待，所以状态是TIMED_WAITING】*/
                t1.join(5000L);
                System.out.println(" t2中执行t1.join()");
                /**（t2）等待t1执行完！【此时t2是无限制时间等待，所以状态是WAITING】*/
                t1.join();

                System.out.println(" t2执行完");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });


        t1.start();
        t2.start();
        /**主线程睡觉1秒钟，保证t2执行到join(5000L)语句了【TIMED_WAITING】*/
        Thread.sleep(1000L);
        System.out.println("t2的状态："+ t2.getState());

        /**主线程睡觉5秒钟，保证t2执行到join()语句了【WAITING】*/
        Thread.sleep(5000L);
        System.out.println("t2的状态："+ t2.getState());
    }
}
